import 'package:get/get.dart';
import '../log_service.dart';
import 'log_cache_model.dart';

// 日志控制器
class LogsController extends GetxController {
  // 日志服务
  final LogService logService = LogService.to;

  // 日志列表
  RxList<LogCacheModel> logList = <LogCacheModel>[].obs;

  @override
  void onInit() async {
    super.onInit();

    logService.getLogs().then((logs) {
      // 按时间升序排列
      logs.sort((a, b) => a.timestamp.compareTo(b.timestamp));
      logList.value = logs;
      logList.refresh();
    }).catchError((error) {
      logService.error('Failed to fetch logs', tag: 'API', stackTrace: error);
    });
  }
}
